#include <QDebug>
#include <QPalette>
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QRectF>
#include "scaningcircle.h"

ScaningCircle::ScaningCircle(QWidget *parent) :
    QWidget(parent)
{
    radius = 50;
    lineWidth = 5;
    lineColor = QColor(255, 255, 255);

    x = 0;
    y = 0;
    scrWidth = parent->width();
    scrHeight = parent->height();
}

void ScaningCircle::setCircle(int r, int lw, QColor lColor, int inter)
{
    radius = r;
    lineWidth = lw;
    lineColor = lColor;
    interval = inter;

    y = -radius*2;
    qDebug() << "Scaning Circle:";
    qDebug() << "Radius:" << radius;
    qDebug() << "Line Width:" << lineWidth;
    qDebug() << "Color:" << lineColor;

    myTimer.start(interval);
    connect(&myTimer, SIGNAL(timeout()), this, SLOT(repaint()));
}

void ScaningCircle::keyPressEvent(QKeyEvent *keyEvent)
{
    if (keyEvent->key()==Qt::Key_Escape)
    {
        this->close();
    }
}

void ScaningCircle::paintEvent(QPaintEvent *)
{
    drawNewCircle();
}

void ScaningCircle::drawNewCircle()
{
    //TODO
#ifdef DEBUG
    qDebug() << "Entering drawNewCircle";
#endif
    this->setAutoFillBackground(true);
    QPalette palette;
    palette.setColor(QPalette::Background, QColor(0,0,0));
    this->setPalette(palette);

    QPainter painter(this);
    QBrush brush;
    QPen pen;

    pen.setWidth(lineWidth);
    brush.setColor(lineColor);
    pen.setColor(lineColor);

    QRectF rect(x, y, radius*2, radius*2);
    qDebug() << "QRectF:" << rect;
    painter.setPen(pen);
    painter.setBrush(brush);
    painter.drawEllipse(rect);

    if (x + radius*2 <= scrWidth)
    {
        if (y + radius*2 < scrHeight){
            y += radius;
        } else {
            if (x + radius*2 < scrWidth){
                x += radius;
                y = 0;
            } else {

            }
        }
    }
}

void ScaningCircle::endThis()
{
    this->close();
}
